/**
 * @brief 通信平台基础头文件
 * @anchor ljl
 * @date 2023
 * @version 1.0
*/
#pragma once
#ifndef PLATFORM_H
#define PLATFORM_H

#include "systemPublic.h"
#include "socketDataParse.h"
#include "worker.h"


using namespace std;

class Platform;
/**
 * @class ClientsInfo
 * @brief
 * @param string id
 * @param string name
 * @param string ip, 唯一标识
 * @param string task
 * @param int connfd, 套接字描述符
 * @param bool use 该通信是否连接使用
 * @param void* selfPtr, Platform*用于任务结束后，处理ClientsInfo指针, 默认：NULL
*/
class ClientsInfo{
public:
    ClientsInfo(){};
    ClientsInfo(const ClientsInfo& self){
        upData(self);
    };
    ~ClientsInfo(){};
    void upData(const ClientsInfo& self){
        unique_lock<mutex> lock(mMutexClientsInfo);
        mType = self.mType;
        mConnfd = self.mConnfd;
        mId = self.mId;
        mIp = self.mIp;
        mPort = self.mPort;
        mName = self.mName;
        mTask = self.mTask;
        mTaskId = self.mTaskId;
        
        mUse = self.mUse;
        mPlatformPtr = self.mPlatformPtr;
        mPool = self.mPool;
    };
    
    string getType(){
        unique_lock<mutex> lock(mMutexClientsInfo);
        return mType;
    };
    void setType(const string& type){
        unique_lock<mutex> lock(mMutexClientsInfo);
        mType = type;
    };

    int getConnfd(){
        unique_lock<mutex> lock(mMutexClientsInfo);
        return mConnfd;
    };
    void setConnfd(const int& connfd){
        unique_lock<mutex> lock(mMutexClientsInfo);
        mConnfd = connfd;
    };

    string getId(){
        unique_lock<mutex> lock(mMutexClientsInfo);
        return mId;
    };
    void setId(const string& id){
        unique_lock<mutex> lock(mMutexClientsInfo);
        mId = id;
    };

    string getIp(){
        unique_lock<mutex> lock(mMutexClientsInfo);
        return mIp;
    };
    void setIp(const string& ip){
        unique_lock<mutex> lock(mMutexClientsInfo);
        mIp = ip;
    };

    int getPort(){
        unique_lock<mutex> lock(mMutexClientsInfo);
        return mPort;
    };
    void setPort(const int& port){
        unique_lock<mutex> lock(mMutexClientsInfo);
        mPort = port;
    };

    string getName(){
        unique_lock<mutex> lock(mMutexClientsInfo);
        return mName;
    };
    void setName(const string& name){
        unique_lock<mutex> lock(mMutexClientsInfo);
        mName = name;
    };

    string getTaskBrief(){
        unique_lock<mutex> lock(mMutexClientsInfo);
        return mTask;
    };
    void setTaskBrief(const string& task){
        unique_lock<mutex> lock(mMutexClientsInfo);
        mTask = task;
    };
    string getTaskId(){
        unique_lock<mutex> lock(mMutexClientsInfo);
        return mTaskId;
    };
    void setTaskId(const string& taskId){
        unique_lock<mutex> lock(mMutexClientsInfo);
        mTaskId = taskId;
    };


    bool getUse(){
        unique_lock<mutex> lock(mMutexClientsInfo);
        return mUse;
    };
    void setUse(const bool& use){
        unique_lock<mutex> lock(mMutexClientsInfo);
        mUse = use;
    };

    Platform* getPlatformPtr(){
        unique_lock<mutex> lock(mMutexClientsInfo);
        return mPlatformPtr;
    };
    void setPlatformPtr(Platform* platformPtr){
        unique_lock<mutex> lock(mMutexClientsInfo);
        mPlatformPtr = platformPtr;
    };

    ThreadPool* getPoolPtr(){
        unique_lock<mutex> lock(mMutexClientsInfo);
        return mPool;
    };
    void setPoolPtr(ThreadPool* pool){
        unique_lock<mutex> lock(mMutexClientsInfo);
        mPool = pool;
    };

    int mSend(const int& connfd, const string& sendInfo){
        unique_lock<mutex> lock(mMutexSend);
        return send(connfd, sendInfo.c_str(), strlen(sendInfo.c_str()) ,0);
    }
    
private:
    std::mutex mMutexSend;
    std::mutex mMutexClientsInfo;

    string mType = "null";
    int mConnfd = -1;
    string mId = "null";    //自身的id
    string mIp = "null";
    int mPort = -1;
    string mName = "null";
    string mTask = "null";
    string mTaskId = "null";
    
    bool mUse = false;
    Platform* mPlatformPtr = NULL;
    ThreadPool* mPool = NULL;

    //智能指针
    std::shared_ptr<int> testI;
    std::unique_ptr<int> test2;
    std::weak_ptr<int> test3;

    
};


/**
 * @brief
 * @param string name = "null", 当name="Exit"时，表示退出总程序， 结束accept循环
 * @param int connfd = -1
 * @param string describe = "null"
*/
struct HelloInfo{
    int connfd = -1;
    string ip = "null";

    string id = "null";
    string name = "null"; 
    string describe = "null";
};

/**
 * @class Platform
 * @brief jlj
*/
class Platform{
    public:
        Platform();
        ~Platform();
        static ClientsInfo*  mGetClientsInfo(const string key);
        static int  mSetClientsInfo(const string key, ClientsInfo* clientInfo);
        static void  mDelClientsInfo(const string key);
        static void  mClearClientsInfo();

        void m_setExit(const bool& val);
        bool m_getExit();
    private:
        void mStart(const PlatformParams params);
        void mJudgetJsonIsExption(const string& info, const string key="");
        static void mAcceptConn(void* arg);
        static string mHelloConnectClient(ClientsInfo* helloInfo);
        static int mExitSocket();
        static void mLogDIWE(const string& info, string logPath="", const LOGGER_::Log_level level=LOGGER_::info);

//......................
    public:
        static string mLogPath;   // xx/xx/xxlog.txt
        static int m_connectOutTime ;  //服务器接收消息的超时时间（非阻塞接收） = 2s
        std::shared_ptr<RT::Worker> mWorkerPtr = NULL;      //通信平台向上位机的通信连接

        std::string workInfo_ = "WorkInfo";
        std::string helloBeat_ = "Hello";
        std::string helloACK_ = "HelloACK";
        std::string heartbeat_ = "Heartbeat";
        std::string heartbeatACK_ = "HeartbeatACK";
        std::string errorInfo_ = "Error";
    private:
        
        std::mutex m_mutexExit;
        bool m_isExit = false;  //

        static std::mutex mMutexClientsInfosMap;
        static int mClientsInfosMapMaxSize;
        static map<string, ClientsInfo*> mClientsInfosMap;  //用户连接最多数量

        string _name = "xx";
        static string mStartTime; // 2023-02-12


        

        

        
};

#endif  //PLATFORM_H 